﻿using System;

namespace AIFSContentManagementSystem.Criteria
{
    public sealed class DateExpression : Expression
    {
        public override ExpressionDataType DataType
        {
            get
            {
                return ExpressionDataType.DATE;
            }
        }

        protected override bool CanCompare(Expression other)
        {
            if (!(other is DateExpression))
                return false;

            DateTime date;

            bool tryParse = DateTime.TryParse(this.ExpressionElement.RightOperand, out date);
            if (!tryParse)
                return false;

            tryParse = DateTime.TryParse(other.ExpressionElement.RightOperand, out date);
            if (!tryParse)
                return false;

            return true;
        }

        protected override int CompareTo(Expression other)
        {
            DateTime thisDate;
            DateTime otherDate;

            DateTime.TryParse(this.ExpressionElement.RightOperand, out thisDate);
            DateTime.TryParse(other.ExpressionElement.RightOperand, out otherDate);

            return thisDate.CompareTo(otherDate);
        }
    }
}
